package nl.hu.verbrandendephoenix.controller;

import nl.hu.verbrandendephoenix.client.AppManager;
import nl.hu.verbrandendephoenix.client.NotificationManager;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Panel;

public class BlockUser {

	private String strUsername;
	public BlockUser(String username) {
		this.strUsername = username;
	}

	public Panel getPanel() {
		return new nl.hu.verbrandendephoenix.view.BlockUser(strUsername, this);
	}

	public ClickHandler hndYes() {
		return new ClickHandler() {
			/**
			 * Na het klikken van 'blokkeer', blokkeer de user.
			 */
			@Override
			public void onClick(ClickEvent event) {
				String loginKey = Cookies.getCookie("loginString");
				
				AppManager.get().getRpcService().blockUser(loginKey, strUsername, new AsyncCallback<Boolean>() {
					@Override
					public void onFailure(Throwable caught) {
						NotificationManager.get().commError();
					}
					@Override
					public void onSuccess(Boolean result) {
						if(result) {
							NotificationManager.get().createNotification("Gebruiker " + strUsername + " is succesvol geblokkeerd.");
							History.back();
						} else {
							NotificationManager.get().commError();
						}
					}
				});
			}
		};
	}
	public ClickHandler hndCancel() {
		return new ClickHandler() {
			/**
			 * Na het klikken van 'annuleren', terug naar vorige pagina.
			 */
			@Override
			public void onClick(ClickEvent event) {
				History.back();
			}
		};
	}
}
